package com.tim.app.ui.activity.person_center;

import android.content.Intent;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.runtime.event.EventManager;
import com.application.library.util.KeyboardChangeListener;
import com.application.library.util.MathUtil;
import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.PrepareExchangeBean;
import com.tim.app.server.result.ResponseBean;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.dialog.TipDialog;
import com.tim.app.util.TextUtil;
import com.tim.app.util.ToastUtil;

/**
 * Created by wdy on 2017/8/22.
 */

public class ExchangeMoneyActivity extends BaseActivity implements KeyboardChangeListener.KeyBoardListener {
    private final String TAG = ExchangeMoneyActivity.class.getSimpleName();
    private ImageButton mIb_back;
    private TextView mTv_moli_num;
    private TextView mTv_exchange_rate;
    private EditText mEt_exchange_num;
    private TextView mTv_exchange_money;
    private TextView mTv_commit;
    private KeyboardChangeListener mKeyboardChangeListener;

    @Override
    protected int getLayoutId() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return R.layout.activity_exchangemoney;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_commit:
                if (TextUtils.isEmpty(mEt_exchange_num.getText().toString().trim()) || 0 == Integer.parseInt(mEt_exchange_num.getText().toString().trim())) {
                    ToastUtil.showToast("请输入兑换数量");
                } else if (Integer.parseInt(mEt_exchange_num.getText().toString().trim()) > date.getMoliFlowerCount()) {
                    ToastUtil.showToast("超过最大数量");
                } else {
                    exchangeMoney();
                }
                break;
            case R.id.ib_back:
                finish();
                break;

        }
    }

    private void exchangeMoney() {
        mTv_commit.setClickable(false);
        showLoadingDialog();
        API_User.ins().exchange(TAG, UserManager.ins().getUserId(), mEt_exchange_num.getText().toString().trim(), new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                mTv_commit.setClickable(true);
                hideLoadingDialog();
                ResponseBean responseBean = new Gson().fromJson(resultValue, ResponseBean.class);
                if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                    //   mTv_moli_num.setText(date.getMoliFlowerCount() - Integer.parseInt(mEt_exchange_num.getText().toString().trim()) + "");
                    mTv_moli_num.setText(Integer.parseInt(mTv_moli_num.getText().toString().trim()) - Integer.parseInt(mEt_exchange_num.getText().toString().trim()) + "");
                    EventManager.ins().sendEvent(EventTag.FLOWER_NUM_CHANGE, Integer.parseInt(mEt_exchange_num.getText().toString().trim()), 0, null);
                    mEt_exchange_num.setText("");
                    mEt_exchange_num.setHint(getString(R.string.num_exchange_limit));
                    //   setResult(RESULT_OK, getIntent().putExtra("exchange_num", Integer.parseInt(mEt_exchange_num.getText().toString().trim())));
                    final TipDialog tipDialog = new TipDialog(ExchangeMoneyActivity.this);
                    tipDialog.setTextDes("兑换成功");
                    tipDialog.setButton1("确定", new TipDialog.DialogButtonOnClickListener() {
                        @Override
                        public void onClick(View button, TipDialog dialog) {
                            tipDialog.dismiss();
                        }
                    });
                    tipDialog.setCancleGone();
                    tipDialog.show();
                } else {
                    ToastUtil.showToast(responseBean.getMessage());
                }

                return false;
            }
        });
    }

    @Override
    public void initView() {
        mIb_back = (ImageButton) findViewById(R.id.ib_back);
        mIb_back.setOnClickListener(this);
        mTv_moli_num = (TextView) findViewById(R.id.tv_moli_num);
        mTv_exchange_rate = (TextView) findViewById(R.id.tv_exchange_rate);
        mEt_exchange_num = (EditText) findViewById(R.id.et_exchange_num);
        mTv_exchange_money = (TextView) findViewById(R.id.tv_exchange_money);
        mTv_commit = (TextView) findViewById(R.id.tv_commit);
        mTv_commit.setOnClickListener(this);
        mEt_exchange_num.addTextChangedListener(textChangedListener);
    }

    private TextWatcher textChangedListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            double i;
            if (TextUtils.isEmpty(s)) {
                //    mEt_exchange_num.setText(0 + "");
                mTv_exchange_money.setText(0 + "元");
            } else {
//                if (Integer.parseInt(s.toString()) > date.getMoliFlowerCount()) {
//                    i = Integer.parseInt(date.getMoliFlowerCount() + "") * date.getMoney() / date.getFlower();
//                    mEt_exchange_num.setText(date.getMoliFlowerCount() + "");
//                } else {
                i = Double.parseDouble(s.toString()) * date.getMoney() / date.getFlower();
//                }
                String value = MathUtil.format(i);
                mTv_exchange_money.setText(value + "元");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    public void initData() {
        PrepareExchangeBean.ResBean.ConfigureflowermoneyBean date = getIntent().getParcelableExtra("date");
        mKeyboardChangeListener = new KeyboardChangeListener(this);
        mKeyboardChangeListener.setKeyBoardListener(this);

        setDate(date);
//                }
//                return false;
//            }
//        });

    }

    private PrepareExchangeBean.ResBean.ConfigureflowermoneyBean date;

    private void setDate(PrepareExchangeBean.ResBean.ConfigureflowermoneyBean date) {
        this.date = date;
        mTv_moli_num.setText(date.getMoliFlowerCount() + "");
        //  今日汇率：&#160;%s朵茉莉花 = &#160;%s元
        String rate = new StringBuilder("今日汇率:").append(date.getFlower()).append("朵茉莉花 =").append(date.getMoney()).append("元").toString();
        mTv_exchange_rate.setText(rate);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(TAG);
    }

    @Override
    public void onKeyboardChange(boolean isShow, int keyboardHeight) {
        if (!isShow) {
            if(TextUtils.isEmpty(mEt_exchange_num.getText().toString().trim())){
                return;
            }
            if (Integer.parseInt(mEt_exchange_num.getText().toString().trim()) > date.getMoliFlowerCount()) {
                mEt_exchange_num.setText(date.getMoliFlowerCount() + "");
            }
            if (!TextUtils.isEmpty(mEt_exchange_num.getText().toString().trim()) && Integer.parseInt(mEt_exchange_num.getText().toString().trim()) % 10 != 0) {
                int i = Integer.parseInt(mEt_exchange_num.getText().toString().trim()) % 10;
                int i1 = Integer.parseInt(mEt_exchange_num.getText().toString().trim()) - i;
                if (date.getMoliFlowerCount() > 10 && Integer.parseInt(mEt_exchange_num.getText().toString().trim()) < 10) {
                    i1 = 10;
                }
                mEt_exchange_num.setText(i1 + "");
            }
        }
    }
}
